package com.ruoyi.back.service.impl

import cn.hutool.core.util.StrUtil
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.ruoyi.app.common.Data
import com.ruoyi.app.common.Ok
import com.ruoyi.back.domain.SpreadCenter
import com.ruoyi.back.domain.dto.SpreadCenterListSearchDto
import com.ruoyi.back.mapper.SpreadCenterMapper
import com.ruoyi.back.service.SpreadCenterService
import com.ruoyi.common.core.page.TableSupport
import org.springframework.stereotype.Service

@Service
class SpreadCenterServiceImpl : ServiceImpl<SpreadCenterMapper, SpreadCenter>(), SpreadCenterService {


    override fun saveSpreadCenter(spreadCenter: SpreadCenter): Data<Boolean> =
        spreadCenter.id?.run {
            Ok("更新成功", updateById(spreadCenter))
        } ?: Ok("保存成功", save(spreadCenter))

    override fun spreadCenterPage(spreadCenterListSearchDto: SpreadCenterListSearchDto): Page<SpreadCenter> =
        TableSupport.buildPageRequest().let { pageDomain ->
            page(Page(pageDomain.pageNum.toLong(), pageDomain.pageSize.toLong()), KtQueryWrapper(SpreadCenter::class.java)
                .eq(SpreadCenter::channelType, spreadCenterListSearchDto.channelType)
                .eq(spreadCenterListSearchDto.location != null && spreadCenterListSearchDto.location != 0, SpreadCenter::location, spreadCenterListSearchDto.location)
                .like(StrUtil.isNotBlank(spreadCenterListSearchDto.appName), SpreadCenter::appName, spreadCenterListSearchDto.appName)
                .orderByAsc(SpreadCenter::orderNum))
        }

    override fun removeSpreadCenter(spreadCenterIds: String): Data<Boolean> = Ok("移除成功", removeByIds(spreadCenterIds.split(",")))
}